//
//  TopicViewController.m
//  Leisure
//
//  Created by xalo on 16/6/30.
//  Copyright © 2016年 蓝鸥科技有限公司西安分公司. All rights reserved.
//

#import "TopicViewController.h"

@interface TopicViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;

@property (weak, nonatomic) IBOutlet UITableView *hotTableView;
@property (weak, nonatomic) IBOutlet UITableView *newestTableView;
@property (nonatomic,strong)UISegmentedControl *segmentedControl;
@property (nonatomic,strong)NSMutableArray *hotDataSource;
@property (nonatomic,strong)NSMutableArray *newestDataSource;
@property (nonatomic,assign)NSInteger hotStart;
@property (nonatomic,assign)NSInteger newStart;
@end

@implementation TopicViewController

- (NSMutableArray *)hotDataSource{
    if (!_hotDataSource) {
        _hotDataSource=[NSMutableArray array];
    }
    return _hotDataSource;
}

- (NSMutableArray *)newestDataSource{
    if (!_newestDataSource) {
        _newestDataSource=[NSMutableArray array];
    }
    return _newestDataSource;
}

- (UISegmentedControl *)segmentedControl{
    if (!_segmentedControl) {
        _segmentedControl=[[UISegmentedControl alloc]initWithFrame:CGRectMake(0, 0, 100, 30)];
        [_segmentedControl insertSegmentWithTitle:@"HOT" atIndex:0    animated:0];
        [_segmentedControl insertSegmentWithTitle:@"NEW" atIndex:1 animated:YES];
        _segmentedControl.selectedSegmentIndex=0;
        
        [_segmentedControl addTarget:self action:@selector(clickSegmentedController:) forControlEvents:UIControlEventValueChanged];
    }
    return _segmentedControl;
}

//点击事件
- (void)clickSegmentedController:(UISegmentedControl *)sender{
    if (sender.selectedSegmentIndex==0) {
        
        [self.scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
        
    }else{
        
        [self.scrollView setContentOffset:CGPointMake(kWidth, 0) animated:YES];
    }
}

#pragma mark------------UIScrollView的协议----------------
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    //判断滚动的是不是你要它滚的
    if (scrollView==self.scrollView) {
        
        if (self.scrollView.contentOffset.x==0) {
            
            self.segmentedControl.selectedSegmentIndex=0;
        }else if (self.scrollView.contentOffset.x==kWidth){
            
            self.segmentedControl.selectedSegmentIndex=1;
        }
    }
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor=[UIColor purpleColor];
    self.navigationItem.titleView=self.segmentedControl;
    self.automaticallyAdjustsScrollViewInsets=NO;
    
    [self.hotTableView registerNib:[UINib nibWithNibName:@"TopicCell" bundle:nil] forCellReuseIdentifier:@"TopicModel_cell"];
    [self.newestTableView registerNib:[UINib nibWithNibName:@"TopicCell" bundle:nil] forCellReuseIdentifier:@"TopicModel_cell"];
    
    //请求数据
    [self loadHotTableViewDataSourceWithStart:0 limit:10];
    [self loadnewestTableViewDataSourceWithStart:0 limit:10];
    //上拉加载
    self.hotTableView.mj_footer=[MJRefreshBackGifFooter footerWithRefreshingBlock:^{
        self.hotStart +=10;
        [self loadHotTableViewDataSourceWithStart:self.hotStart limit:10];
    }];
    self.newestTableView.mj_footer=[MJRefreshBackGifFooter footerWithRefreshingBlock:^{
        self.newStart +=10;
        [self loadnewestTableViewDataSourceWithStart:self.newStart limit:10];
    }];
    //下拉刷新
    self.newestTableView.mj_header=[MJRefreshStateHeader headerWithRefreshingBlock:^{
        //刷新之前清空数组
        [self.newestDataSource removeAllObjects];
        self.newStart = 0;
        [self loadnewestTableViewDataSourceWithStart:0 limit:10];
        
    }];
    self.hotTableView.mj_header=[MJRefreshStateHeader headerWithRefreshingBlock:^{
        //刷新之前清空数组
        [self.hotDataSource removeAllObjects];
        self.hotStart = 0;
        [self loadHotTableViewDataSourceWithStart:0 limit:20];
    }];
}

- (void)loadHotTableViewDataSourceWithStart:(NSInteger)start limit:(NSInteger)limit{
    [RequestManager fetchDataWithUrl:kTopicUrl parameter:@{@"contentid":@"self.contentid",@"start":@(start),@"limit":@(limit),@"sort":@"hot"} method:@"POST" compelet:^(NSData *data) {
        NSDictionary *bigDic=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
       // NSLog(@"%@",bigDic);
        for (NSDictionary *smallDic in bigDic[@"data"][@"list"]) {
            TopicModel *model=[[TopicModel alloc]init];
            [model setValuesForKeysWithDictionary:smallDic];
            [self.hotDataSource addObject:model];
        }
    } updateUI:^{
        //结束上拉加载
        [self.hotTableView.mj_footer endRefreshing];
        [self.hotTableView.mj_header endRefreshing];
        
        [self.hotTableView reloadData];
    }];
    
}

- (void)loadnewestTableViewDataSourceWithStart:(NSInteger)start limit:(NSInteger)limit{
    [RequestManager fetchDataWithUrl:kTopicUrl parameter:@{@"contentid":@"self.contentid",@"start":@(start),@"limit":@(limit),@"sort":@"addtime"} method:@"POST" compelet:^(NSData *data) {
        NSDictionary *bigDic=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
         NSLog(@"%@",bigDic);
        for (NSDictionary *smallDic in bigDic[@"data"][@"list"]) {
            TopicModel *model=[[TopicModel alloc]init];
            [model setValuesForKeysWithDictionary:smallDic];
            [self.newestDataSource addObject:model];
           // NSLog(@"%@",self.newestDataSource);
        }
    } updateUI:^{
        //结束上拉加载
          [self.newestTableView.mj_footer endRefreshing];
        [self.newestTableView.mj_header endRefreshing];
        [self.newestTableView reloadData];
    }];
    
}

#pragma  mark-------------UITableView的协议-----------------

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (tableView==self.hotTableView) {
        return self.hotDataSource.count;
    }else{
        return self.newestDataSource.count;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  
    TopicModel *model=[[TopicModel alloc]init];
    if (tableView==self.hotTableView) {
        model=self.hotDataSource[indexPath.row];
    }else{
        model=self.newestDataSource[indexPath.row];
    }
    
    if (model.content.length==0) {
        return 150;
    }
    //文字自适应高度
    CGRect rect = [model.content boundingRectWithSize:CGSizeMake(kWidth - 20, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]} context:nil];
   
    
    return  rect.size.height+132;
    
    
    
    
    
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    TopicCell *cell=[tableView dequeueReusableCellWithIdentifier:@"TopicModel_cell" forIndexPath:indexPath];
    
   if (tableView==self.hotTableView) {
        
        [cell setCellWithModel:self.hotDataSource[indexPath.row]];
    
    }else{
        [cell setCellWithModel:self.newestDataSource[indexPath.row]];
    }
    //创建一个model来接收
//    TopicModel *model=[[TopicModel alloc]init];
//    if (tableView==self.hotTableView) {
//        model=self.hotDataSource[indexPath.row];
//    }else{
//        model=self.newestDataSource[indexPath.row];
//    }
//    //工厂模式产出来的cell
//    
//    BaseCell *cell=[FactoryMode productCellWithModel:model tableView:tableView indexPath:indexPath];

    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    TopicListViewController *topicListVC=[[TopicListViewController alloc]init];
    if (tableView ==self.hotTableView) {
        topicListVC.contentID=[self.hotDataSource[indexPath.row] contentid];
        
    }else{
        topicListVC.contentID=[self.newestDataSource[indexPath.row] contentid];
    }
    
    [self.navigationController pushViewController:topicListVC animated:YES];
}


























- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
  
    }

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
